﻿package com.wly.ui.utils
{
	import flash.display.DisplayObject;
	import flash.geom.Point;
	import flash.geom.Rectangle;

	public class CommonUtils
	{
		static public const HIT_NONE:int = 0;
		static public const HIT_LEFT:int = 1;
		static public const HIT_UP:int   = 2;
		static public const HIT_RIGHT:int= 3;
		static public const HIT_DOWN:int = 4;
		
		static public function doHitCheck(objSrc:DisplayObject, objDst:DisplayObject):int
		{
			if(!objDst || !objSrc)
			{
				return HIT_NONE;
			}
			
			if(!objDst.root || !objSrc.root)
			{
				return HIT_NONE;
			}
			
			var srcRect:Rectangle = objSrc.getBounds(objSrc.root);
			var dstRect:Rectangle = objDst.getBounds(objDst.root);
			
			if(srcRect.intersects(dstRect))
			{
				//trace("doHitCheck", objSrc.name, srcRect, objDst.name, dstRect);
				var hitRect:Rectangle = srcRect.intersection(dstRect);
				//trace("doHitCheck", hitRect);
				if(hitRect.width>hitRect.height)
				{
					if(srcRect.top>dstRect.top)
					{
						return HIT_UP;
					}
					else
					{
						return HIT_DOWN;
					}
				}
				else
				{
					if(srcRect.left<dstRect.left)
					{
						return HIT_RIGHT;
					}
					else
					{
						return HIT_LEFT;
					}
				}
			}
			
			return 0;
		}
	}
}